package com.example;

import java.util.ArrayList;
import java.util.List;

import com.example.R;
import com.example.linerlistview.LinearListView;
import com.example.linerlistview.LinearListView.OnItemClickListener;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("NewApi")
public class Main extends Activity implements
		SwipeRefreshLayout.OnRefreshListener {
	List<String> list = new ArrayList();

	private BaseAdapter mAdapter = new BaseAdapter() {

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = getLayoutInflater().inflate(R.layout.list_item,
						parent, false);
			}
			((TextView) convertView).setText("Position " + position);
			return convertView;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public int getCount() {
			return list.size();
		}
	};

	OnItemClickListener mListener = new OnItemClickListener() {

		@Override
		public void onItemClick(LinearListView parent, View view, int position,
				long id) {
			Toast.makeText(getApplicationContext(),
					"Tapped position " + position, Toast.LENGTH_SHORT).show();

		}
	};

	private int mCount = 50;

	private SwipeRefreshLayout mSwipeLayout;

	@SuppressLint("NewApi")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		LinearListView vertical = (LinearListView) findViewById(R.id.vertical_list);
		vertical.setDividerDrawable(new ColorDrawable(Color.CYAN));
		vertical.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE
				| LinearLayout.SHOW_DIVIDER_BEGINNING);
		vertical.setDividerThickness(getResources().getDimensionPixelSize(
				R.dimen.padding_small));
		for (int i = 0; i < mCount; i++) {
			list.add(new String());
		}
		vertical.setAdapter(mAdapter);
		vertical.setOnItemClickListener(mListener);

		mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
		mSwipeLayout.setOnRefreshListener(this);
		mSwipeLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
				android.R.color.holo_green_light,
				android.R.color.holo_orange_light,
				android.R.color.holo_red_light);
	}

	public void onRefresh() {
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				mSwipeLayout.setRefreshing(false);

				list.clear();
				mCount = (int) (Math.random() * 20);
				for (int i = 0; i < mCount; i++) {
					list.add(new String());
				}
				mAdapter.notifyDataSetChanged();
			}
		}, 1000);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == R.id.menu_change) {
			mCount = (int) (Math.random() * 50);
			mAdapter.notifyDataSetChanged();
			return true;
		} else
			return super.onOptionsItemSelected(item);
	}
}
